package com.xcity.game.item;

import com.xcity.db.entity.Player;
import com.xcity.game.common.Cause;
import com.xcity.game.common.MessageConst;
import com.xcity.game.event.GameEvents;
import com.xcity.game.item.listener.DefaultBagListener;
import com.xcity.game.item.operation.SellItemOperation;
import com.xcity.game.player.cache.PlayerCacheService;
import com.xcity.pomelo.PomeloRoute;
import com.xcity.pomelo.request.BagInfoRequest;
import com.xcity.pomelo.request.GetUglyItemInfoRequest;
import com.xcity.pomelo.request.SellItemRequest;
import com.xcity.pomelo.request.UseItemRequest;
import com.xcity.pomelo.response.BagInfoResponse;
import com.xcity.pomelo.response.CheckItemTimeResponse;
import com.xcity.pomelo.response.HintResponse;
import com.xcity.pomelo.response.Response;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.net.Session;
import naga.x.net.annotation.OPHandler;
import naga.x.net.impl.pomelo.annotation.PomeloOP;
import naga.x.service.Service;
import naga.x.service.ServiceException;

@OPHandler
public class ItemService implements Service {
	
	public static ItemTemplate goldDefault, stoneDefault, addSkillPointsDefault, honorDefault;
	
	@Override
	public void startup() throws ServiceException {
		TemplateService ts = App.getApp().getServiceManager().get(TemplateService.class);
		goldDefault = ts.get(ItemTemplate.class, 20023);
		stoneDefault = ts.get(ItemTemplate.class, 20017);
		addSkillPointsDefault = ts.get(ItemTemplate.class, 61030001);
		honorDefault = ts.get(ItemTemplate.class, 61060001);
	}

	@Override
	public void shutdown() throws ServiceException {
	}

	@Override
	public String getId() {
		return getClass().getName();
	}
	
	@Deprecated
	@PomeloOP(PomeloRoute.CHECK_ITEM_TIME)
	protected void checkItemTime(Session session, GetUglyItemInfoRequest req) {
		Player player = session.getClient();
		session.send(new CheckItemTimeResponse(req.getSerial(), player));
	}
	
	@PomeloOP(PomeloRoute.BAG_INFO)
	protected void requestBagInfo(Session session, BagInfoRequest req) {
		Player player = session.getClient();
		session.send(new BagInfoResponse(req.getSerial(), player.getBag()));
	}
	
	@PomeloOP(PomeloRoute.USE_ITEM)
	protected void requestUseItem(Session session, UseItemRequest req) {
		if (req.getCount() <= 0) {
			session.send(new HintResponse(req.getSerial(), MessageConst.ILLEGAL_OPERATION));
			return;
		}
		Player player = session.getClient();
		GameItem item = player.getBag().getItem(req.getId());
		if (item == null) {
			session.send(new HintResponse(req.getSerial(), MessageConst.ITEM_NOT_EXIST));
			return;
		}
		Player target;
		if (req.getTarget() != 0) {
			target = App.getApp().getServiceManager().get(PlayerCacheService.class).find(req.getTarget());
			if (target == null) {
				session.send(new HintResponse(req.getSerial(), MessageConst.PLAYER_NOT_EXIST));
				return;
			}
		} else {
			target = player;
		}
//		// 二次确认
//		UseItemOperation op = new UseItemOperation(
//				player,
//				target,
//				req.getId(),
//				req.getCount(),
//				MessageFormat.format(
//						item.getTemplate().getEffect() == ItemEffect.ADD_CHARM // 送玫瑰
//						? MessageConst.PRESENT_ITEM_CONFIRM : MessageConst.USE_ITEM_CONFIRM,
//								req.getCount(),
//								item.getName())
//				);
//		op.send();
		try {
			DefaultBagListener bagLsr = new DefaultBagListener();
			item = player.getBag().useItem(target, item.getId(), req.getCount(), Cause.USE_ITEM, bagLsr);
			if (item != null) {
				App.getApp().getEventManager().addEvent(GameEvents.EVENT_USE_ITEM, player, item.getTemplate(), req.getCount(), target);
			}
			bagLsr.end(player);
			session.send(new Response(req.getSerial()));
		} catch (ItemException e) {
			player.send(new HintResponse(req.getSerial(), e.getMessage()));
		}
	}
	
	@PomeloOP(PomeloRoute.SELL_ITEM)
	protected void requestSell(Session session, SellItemRequest req) {
		int count = req.getCount();
		long instanceId = req.getId();
		if (count <= 0 || instanceId == 0) {
			session.send(new HintResponse(req.getSerial(), MessageConst.ILLEGAL_OPERATION));
			return;
		}
		Player player = session.getClient();
		// 先当做模板id获取一下
		ItemTemplate template = App.getApp().getServiceManager().get(TemplateService.class).get(ItemTemplate.class, (int) instanceId);
		if (template != null) {
			instanceId = 0L;
		} else {
			GameItem item = player.getBag().getItem(instanceId);
			if (item == null) {
				session.send(new HintResponse(req.getSerial(), MessageConst.ITEM_NOT_EXIST));
				return;
			}
			template = item.getTemplate();
		}
		// 不用二次确认
//		if (template.getQuality() < 2) { // 品质2以下不需要二次确认
			String error = SellItemOperation.sell(player, template, instanceId, count);
			if (error != null) {
				session.send(new HintResponse(req.getSerial(), error));
				return;
			}
//		} else {
//			ConfirmOperation op = new SellItemOperation(player, template, instanceId, req.getCount(), MessageFormat.format(MessageConst.ITEM_SELL_HINT, count, template.getName()));
//			op.send();
//		}
		session.send(new Response(req.getSerial()));
	}

}
